//
//  UIBarButtonitem+Extension.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    
    /// 自定义导航按钮
    ///
    /// - parameter imageName:   默认图片
    /// - parameter bgImageName: 背景图片
    ///
    /// - returns: 导航按钮
    convenience init(imageName: String,bgImageName: String,target: Any?,action:Selector) {
        //初始化按钮
        let btn = UIButton(type: UIButtonType.custom)
        //先获得图片
        let nomoImage = UIImage(named: imageName)
        //获得图片尺寸
        let imgSize = nomoImage?.size ?? CGSize()
        //根据图片尺寸设置frame
        btn.frame = CGRect(x: 0, y: 0, width: imgSize.width, height: imgSize.height)
        
        btn.setBackgroundImage(nomoImage, for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: bgImageName), for: UIControlState.highlighted)
        //点击事件
        btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        //添加按钮到初始化方法
        self.init(customView: btn)
    }
    
}
